using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CellarDoor.Gui.Elements
{
    /// <summary>
    /// A Combobox that can hold a collection objects.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class UiComboBox : UiElement
    {
        private UiElement box;
        private UiButton button;
        private UiListBox list;

        /// <summary>
        /// Gets and sets the width and height of the combobox button
        /// </summary>
        public int ButtonSize 
        {
            get { return (int)button.Width; }
            set 
            {
                button.Height = button.Width = value;
                box.Height = value;
                Invalidate(InvalidBits.Size);
            } 
        }

        /// <summary>
        /// Gets and sets the height of the combobox list
        /// </summary>
        public int ListSize
        {
            get { return (int)list.Height; }
            set
            {
                list.Height = value;
                Invalidate(InvalidBits.Size);
            }
        }

        /// <summary>
        /// Gets the selected item.
        /// </summary>
        public object SelectedItem
        {
            get { return list.SelectedItem; }
        }

        /// <summary>
        /// Creates a new ComboBox.
        /// </summary>
        /// <param name="font"></param>
        public UiComboBox()
            : base()
        {
            Initialize();
        }

        /// <summary>
        /// Creates a new ComboBox.
        /// </summary>
        /// <param name="font"></param>
        public UiComboBox(UiElement element)
            : base(element)
        {
            Initialize();
        }

        private void Initialize()
        {
            box = new UiElement(this);
            box.ElementName = "Panel1";
            box.Padding = new UiPadding(1);
            box.Align = UiAlignStyle.Left | UiAlignStyle.Top;
            box.TextAlign = UiAlignStyle.Middle | UiAlignStyle.Left;

            button = new UiButton(this);
            button.ElementName = "ButtonDown";
            button.Align = UiAlignStyle.Right | UiAlignStyle.Top;
            button.Click += button_MouseClick;

            list = new UiListBox(this);
            list.Padding = new UiPadding(2);
            list.Visible = false;
            list.Enabled = false;
            list.Align = UiAlignStyle.Left | UiAlignStyle.OutsideBottom;
            list.SelectedItemChanged += new EventHandler(list_SelectedItemChanged);
            
            ButtonSize = 20;
            ListSize = 150;
            Width = 120;
        }

        #region Methods
        /// <summary>
        /// Sets the Textproperty to new tems string. Rolls the list in.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void list_SelectedItemChanged(object sender, EventArgs e)
        {
            list.Visible = false;
            list.Enabled = false;
            MakeText();
        }

        void MakeText()
        {
            if (list.SelectedItem != null)
            {
                box.Text = list.SelectedItem.ToString();
            }
            else
            {
                box.Text = string.Empty;
            }
        }

        /// <summary>
        /// Rolls the list in or out.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void button_MouseClick(object sender, EventArgs e)
        {
            RollInAndOut();
        }

        /// <summary>
        /// Adds an item to the collection.
        /// </summary>
        /// <param name="item"></param>
        public void Add(object item)
        {
            list.Add(item);
        }

        /// <summary>
        /// Removes an item from collection
        /// </summary>
        /// <param name="item"></param>
        public void Remove(object item)
        {
            list.Remove(item);
        }

        /// <summary>
        /// Rolls the list in and out.
        /// </summary>
        private void RollInAndOut()
        {
            list.Enabled = !list.Visible;
            list.Visible = !list.Visible;
            list.ScrollH.Value = 0;
            list.ScrollV.Value = 0;
            MakeText();
        }

        protected override void UpdateElementSize()
        {
            base.UpdateElementSize();

            box.Width = Width - button.Width;
            box.Height = button.Height;
            list.Width = Width;
            list.Height = ListSize;
        }

        #endregion
    }
}
